package org.xm.sk.vo.tb;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.xm.sk.vo.Base;
import org.xm.sk.vo.e.Gender;

import com.fasterxml.jackson.annotation.JsonIgnore;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
@Table(name = "dc_user")
@ApiModel(value = "User", parent = Base.class, description = "用户")
public class User extends Base {

    /**
     * 
     */
    private static final long serialVersionUID = -3926742168287198823L;

    @ApiModelProperty(value = "用户编码")
    @Column(length = 255, unique = true)
    private String code;

    @ApiModelProperty(value = "用户名称")
    @Column(length = 255, unique = true)
    private String name;

    @ApiModelProperty(value = "用户所属机构")
    @JsonIgnore
    @ManyToOne(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinColumn(name = "dc_organ_id")
    private Organ organ;

    @ApiModelProperty(value = "用户伲名")
    @Column(length = 255)
    private String nickname;

    @ApiModelProperty(value = "账号密码")
    @Column(length = 255)
    private String psw;

    @ApiModelProperty(value = "用户手机号")
    @Column(length = 20)
    private String phone;

    @ApiModelProperty(value = "用户最近登录时间")
    @Temporal(TemporalType.TIMESTAMP)
    @Column
    @NotNull
    private Date lastPasswordResetDate = new Date();

    @ApiModelProperty(value = "用户的订单")
    @JsonIgnore
    @OneToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "user")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Boder> boders = new HashSet<Boder>(0);

    @ApiModelProperty(value = "用户的优惠券")
    @JsonIgnore
    @OneToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "user")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Coupon> coupons = new HashSet<Coupon>(0);

    @ApiModelProperty(value = "用户关联发型师")
    @JsonIgnore
    @OneToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "user")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Hairdresser> hairdressers = new HashSet<Hairdresser>(0);

    @ApiModelProperty(value = "用户所属门店")
    @JsonIgnore
    @ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinTable(name = "dc_shop_admin", joinColumns = {
            @JoinColumn(name = "dc_user_id") }, inverseJoinColumns = {
                    @JoinColumn(name = "dc_shop_id") })
    @Fetch(FetchMode.SUBSELECT)
    private Set<Shop> shops = new HashSet<Shop>(0);

    @ApiModelProperty(value = "用户借调机构")
    @JsonIgnore
    @ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinTable(name = "dc_allot", joinColumns = {
            @JoinColumn(name = "dc_user_id") }, inverseJoinColumns = {
                    @JoinColumn(name = "dc_organ_id") })
    @Fetch(FetchMode.SUBSELECT)
    private Set<Organ> allot_organs = new HashSet<Organ>(0);

    @ApiModelProperty(value = "用户角色")
    @JsonIgnore
    @ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinTable(name = "dc_user_role", joinColumns = {
            @JoinColumn(name = "dc_user_id") }, inverseJoinColumns = {
                    @JoinColumn(name = "dc_role_id") })
    @Fetch(FetchMode.SUBSELECT)
    private Set<Role> roles = new HashSet<Role>(0);

    @ApiModelProperty(value = "用户头像")
    @JsonIgnore
    @OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
    @JoinColumn(name = "dc_image_id")
    private Image image;

    @ApiModelProperty(value = "用户关注的门店")
    @JsonIgnore
    @ManyToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "fans")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Shop> atShops = new HashSet<Shop>(0);

    @ApiModelProperty(value = "用户关注的发型师")
    @JsonIgnore
    @ManyToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "fans")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Hairdresser> atHairdressers = new HashSet<Hairdresser>(0);

    @ApiModelProperty(value = "用户签到")
    @JsonIgnore
    @OneToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "user")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Sign> sign = new HashSet<Sign>(0);
    
    @ApiModelProperty(value = "用户的评论")
    @JsonIgnore
    @OneToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "user")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Mete> metes = new HashSet<Mete>(0);

    // --------------------- 附加字段 ---------------------
    @ApiModelProperty(value = "用户性别")
    @Enumerated(value = EnumType.ORDINAL)
    @Column
    private Gender gender = Gender.MAN;

    @ApiModelProperty(value = "使用语言")
    @Column(length = 255)
    private String language;

    @ApiModelProperty(value = "所在城市")
    @Column(length = 255)
    private String city;

    @ApiModelProperty(value = "所在省州")
    @Column(length = 255)
    String province;

    @ApiModelProperty(value = "所在国籍")
    @Column(length = 255)
    private String country;

    @ApiModelProperty(value = "用户管辖的门店ID")
    @Transient
    private Integer shopId;

    @ApiModelProperty(value = "用户管辖的门店名称")
    @Transient
    private String shopName;

    @ApiModelProperty(value = "用户角色ID集")
    @Transient
    private Set<Integer> roleIds = new HashSet<Integer>(0);

    @ApiModelProperty(value = "用户角色别名集")
    @Transient
    private String roleNames;

    @ApiModelProperty(value = "用户头像图片ID")
    @Transient
    private Integer imageId;
/*
    
     * 解决微信昵称带图片（非字符内容）的数据库保存问题
     
    public String getNickname() {
        if (null != nickname && 0 < nickname.trim().length())
            try {
                return new String(ToolsUtil.decodeBase64(this.nickname),
                        "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        return null;
    }

    
     * 解决微信昵称带图片（非字符内容）的数据库保存问题
     
    public void setNickname(String nickname) {
        if (null != nickname && 0 < nickname.trim().length())
            try {
                this.nickname = ToolsUtil
                        .encodeBase64(nickname.getBytes("UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
*/
}
